package com.sxz.sbs.module.staff;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.event.OperateSuccessEvent;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.staff.adapter.ChoseStaffAdapter;
import com.sxz.sbs.module.staff.mode.StaffDisposeBean;
import com.sxz.sbs.module.staff.mode.StaffIntentBean;
import com.sxz.sbs.module.staff.presenter.StaffDisposePresenter;
import com.sxz.sbs.module.staff.view.StaffDisposeView;
import com.sxz.sbs.widget.CommonTitle;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 选择员工进行处理
 */
public class ChoseStaffActivity extends BaseActivity<StaffDisposePresenter> implements StaffDisposeView{


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private ChoseStaffAdapter adapter;
    //离职进入
    public static final int CHOSE_STAFF_DIMISSION = 1;
    //案场内交换
    public static final int CHOSE_STAFF_SWAP_IN = 2;
    //案场间交换
    public static final int CHOSE_STAFF_SWAP_OUT = 3;
    private int tag;
    private List<StaffDisposeBean.ListBean> dataList = new ArrayList<>();
    private StaffIntentBean staffBean;
    private int currentPage = 1;
    private boolean isList = true;
    @Override
    protected void init() {
        staffBean = getIntent().getParcelableExtra("staff_bean");
        tag = getIntent().getIntExtra("chose_tag",CHOSE_STAFF_SWAP_IN);
        if (staffBean != null) {
            new CommonTitle.Builder(this).setTitle(staffBean.getParkingName()+"员工").build().show();

            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            refreshLayout.setColorSchemeResources(R.color.color_main);
            refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    currentPage = 1;
                    getStaff();
                }
            });
            refreshLayout.setRefreshing(true);
            getStaff();
            adapter = new ChoseStaffAdapter(dataList);
            recyclerView.setAdapter(adapter);
            recyclerView.addOnItemTouchListener(new OnItemClickListener() {
                @Override
                public void onSimpleItemClick(BaseQuickAdapter adapter1, View view, int position) {
                    adapter.setPressIndex(position);
                }
            });
            adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {
                    currentPage ++;
                    getStaff();
                }
            },recyclerView);
            stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
                @Override
                public void onRetry(int state) {
                    getStaff();
                }
            });


        }

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_chose_staff;
    }
    /**
     * 获取员工列表
     */
    private void getStaff() {
        isList = true;
        HashMap<String,Object> map = HttpParamMap.getParamMap();
        if (tag == CHOSE_STAFF_SWAP_OUT) {
            map.put("parkingId",staffBean.getSwapParkingId());
        } else  {
            map.put("parkingId",staffBean.getParkingId());
        }
        map.put("pageNumber", currentPage);
        map.put("pageSize", AppConstant.PAGE_SIZE);
        mPresenter.getStaffByPId(map);

    }
    @Override
    protected StaffDisposePresenter createPresenter() {
        return new StaffDisposePresenter();
    }

    @OnClick(R.id.btn_sure_chose_staff)
    public void onViewClicked() {
        if (-1 == adapter.getSelectIndex()) {
            ToastUtils.showShort("最少选择一个员工");
            return;
        }
        switch (tag) {
            case CHOSE_STAFF_DIMISSION:
                Intent intent = new Intent();
                intent.putExtra("dimission_id",dataList.get(adapter.getSelectIndex()).getId());
                intent.putExtra("dimission_name",dataList.get(adapter.getSelectIndex()).getTxt_Name());
                setResult(1003,intent);
                finish();
                break;
            case CHOSE_STAFF_SWAP_IN:
                if (staffBean != null) {
                    if (staffBean.getDisposeId() == dataList.get(adapter.getSelectIndex()).getId()) {
                        ToastUtils.showShort("怎么能选自己呢");
                        return;
                    }
                    isList = false;
                    HashMap<String, Object> map = HttpParamMap.getParamMap();
                    map.put("fid", staffBean.getDisposeId());
                    map.put("tid", dataList.get(adapter.getSelectIndex()).getId());
                    map.put("parkingId", staffBean.getParkingId());
                    mPresenter.staffSwapInner(map);
                }
                break;
            case CHOSE_STAFF_SWAP_OUT:
                if (staffBean != null) {
                    isList = false;
                    HashMap<String, Object> map = HttpParamMap.getParamMap();
                    map.put("fid", staffBean.getDisposeId());
                    map.put("fpid", staffBean.getParkingId());
                    map.put("tid", dataList.get(adapter.getSelectIndex()).getId());
                    map.put("tpid", staffBean.getSwapParkingId());
                    mPresenter.staffSwapOut(map);
                }

                break;
            default:
                break;
        }

    }


    @Override
    public void onGetStaffSuccess(StaffDisposeBean bean) {
        if (mIsLive && bean != null) {
            refreshLayout.setRefreshing(false);
            if (bean.getList() != null && bean.getList().size() >0 ) {
                stateLayout.showContent();
                if (currentPage == 1) {
                    dataList.clear();
                    dataList.addAll(bean.getList());
                    if (adapter.getSelectIndex() >= dataList.size()) {
                        adapter.setPressIndex(-1);
                    } else {
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    dataList.addAll(bean.getList());
                    adapter.notifyDataSetChanged();
                }
                adapter.loadMoreComplete();
            } else {
                if (currentPage == 1) {
                    dataList.clear();
                    adapter.notifyDataSetChanged();
                    //  数据为空
                    stateLayout.showEmpty();
                } else {
                    if (dataList.size() > AppConstant.PAGE_SIZE) {
                        adapter.loadMoreEnd();
                    } else {
                        adapter.loadMoreEnd(true);
                    }
                }
            }

        }
    }

    @Override
    public void onSwapSuccess() {
        EventBus.getDefault().post(new OperateSuccessEvent());
        startActivity(OperateSuccessActivity.class);
        finish();
    }

    @Override
    public void showError(int errorTag, String msg) {
        if (isList) {
            refreshLayout.setRefreshing(false);
            if (currentPage == 1) {
                if (errorTag == AppConstant.NET_ERROR_CODE) {
                    stateLayout.showNetwork();
                } else {
                    stateLayout.showEmpty();
                }
            } else {
                adapter.loadMoreFail();
            }
        } else {
            super.showError(errorTag,msg);
        }
    }
    @Override
    public void loginAgain() {
        super.loginAgain();
        if (isList) {
            refreshLayout.setRefreshing(false);
            adapter.loadMoreFail();
        }
    }
}
